
const util=require('../../../utils/checkidcard.js');
var api = require('../../../config/api.js');//接口api地址
var apiutil = require('../../../utils/apiutil.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showbasicinformation:false,
    showinauguration:false,
    showupload:false,
    currentWordNumber:0,
    max:150,
    min:0,
    columns: ["请选择性别","未知", "男", "女"],
    gender:0|| wx.getStorageSync("gender") * 1,
    idcardcolumns: ["请选择证件类型","身份证","港澳居民身份证"],
    idcard: 0 || wx.getStorageSync("idcard") * 1,
    photos1: "",
    photos2: "",
    photos3: "",
    showphotos1: true,
    showphotos2: true,
    showphotos3: true,
    //表单信息
    username:"",
    phone:"",
    idnumber:"",
    useremail:"",
    organizename:"",//单位
    department:"", //科室
    position:"", //职位
    doctorcer:"",//医生资格证
    professioncer:"",//职业证书
    idimage:"",//身份证
    introduction:""//个人简介

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //名称
  inputusername:function(e){
    this.setData({
      username: e.detail.value
    });
  },
  //手机
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    });
  },
  //证件号码
  inputidcard: function (e) {
    this.setData({
      idnumber: e.detail.value
    });
  },
  //邮箱
  inputuseremail: function (e) {
    this.setData({
      useremail: e.detail.value
    });
  },
  //单位
  inputorganizename: function (e) {
    this.setData({
      organizename: e.detail.value
    });
  },
  //科室
  inputdepartment: function (e) {
    this.setData({
      department: e.detail.value
    });
  },
  //职位
  inputposition: function (e) {
    this.setData({
      position: e.detail.value
    });
  },
  //性别选择
  pickSex: function (e) {
    this.setData({
      gender: e.detail.value
    });
    // console.log("当前选择性别-sex", e.detail.value);
  },
  //身份证选择
  pickIdCard: function (e) {
    this.setData({
      idcard: e.detail.value
    });
  },
  //预览图片
  clickImg: function (e) {
    var imgUrl = this.data.photos1;
    console.log("选择图片："+imgUrl)
    wx.previewImage({
      urls: imgUrl, //需要预览的图片http链接列表，注意是数组
      current: this.data.photos1, // 当前显示图片的http链接，默认是第一个  所有要预览的图片的地址集合 数组形式
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
  },
  //上传医生资格证
  uploadcertificate:function(e){
    var that = this
    console.log(e)
    var no = e.currentTarget.id;//id是通过在image设置id属性决定的
    if (no == "1") {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          that.setData({
            photos1: tempFilePaths,
            showphotos1: false
          })
          console.log("选择照片为：" + that.data.photos1)

        }
      })
    } else if (no == "2") {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          that.setData({
            photos2: tempFilePaths,
            showphotos2: false
          })

        }
      })

    } else if (no == "3") {
      wx.chooseImage({
        count: 1, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          var tempFilePaths = res.tempFilePaths
          that.setData({
            photos3: tempFilePaths,
            showphotos3: false
          })
        }
      })

    }
  },
  //字数限制   
  inputs: function (e) {
    // 获取输入框的内容
    var value = e.detail.value;
    // 获取输入框内容的长度
    var len = parseInt(value.length);
    console.log(len)
    //最少字数限制
    if (len <= this.data.min)
      this.setData({
        texts: "至少还需要",
        textss: "字",
        num: this.data.min - len
      })
    else if (len > this.data.min)
      this.setData({
        texts: " ",
        textss: " ",
        num: ''
      })

    this.setData({
      currentWordNumber: len //当前字数  
    });
    //最多字数限制
    if (len > this.data.max) return;
    // 当输入框内容的长度大于最大长度限制（max)时，终止setData()的执行
    this.setData({
      introduction: e.detail.value
    });
    console.log(this.data)
  },
  //收起
  packupbasicinformation:function(){
    this.setData({
      showbasicinformation: !this.data.showbasicinformation
    })
  },
  //展开
  showbasicinformation:function(){
    this.setData({
      showbasicinformation: !this.data.showbasicinformation
    })
    console.log(this.data.show)
  },
  //收起
  packupinauguration: function () {
    this.setData({
      showinauguration: !this.data.showinauguration
    })
  },
  //展开
  showinauguration: function () {
    this.setData({
      showinauguration: !this.data.showinauguration
    })
    console.log(this.data.show)
  },
  //收起
  packupupload: function () {
    this.setData({
      showupload: !this.data.showupload
    })
  },
  //展开
  showupload: function () {
    this.setData({
      showupload: !this.data.showupload
    })
    console.log(this.data.show)
  },


  /**
   * 聚焦函数
   */
  inputFocus: function (t) {
    this.setData({
      [t.target.dataset.id]: "changed"
    });
  },

  /**
   * 失焦函数
   */
  inputBlur: function (t) {
    this.setData({
      [t.target.dataset.id]: t.detail.value.length ? "nick" : ""
    });
  },

  //表单提交
  formSubmit:function(e){
    var that=this;
    var value=e.detail.value
    var photos1=that.data.photos1;
    var photos2 = that.data.photos2;
    var photos3 = that.data.photos3;
    var name=value.username;
    var phone=value.phone;
    var idnumber=value.idnumber;
    var sex = that.data.columns[that.data.gender]; //性别
    var email=value.useremail;
    var organizename=value.organizename; //单位
    var department=value.department; //科室
    var position=value.position; //职位
    var introduction=value.introduction;//个人简介
    // debugger;
   
    if(name==""){
      wx.showToast({
        title: '请输入姓名！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (phone == "") {
      wx.showToast({
        title: '请输入手机号码！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (idnumber == "") {
      wx.showToast({
        title: '请输入证件号码！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (sex == 0) {
      wx.showToast({
        title: '请选择性别！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (email == "") {
      wx.showToast({
        title: '请输入邮箱！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (organizename == "") {
      wx.showToast({
        title: '请输入单位！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (department == "") {
      wx.showToast({
        title: '请输入科室！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (position == "") {
      wx.showToast({
        title: '请输入职位！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (introduction == "") {
      wx.showToast({
        title: '请输入个人简介！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (photos1 == "") {
      wx.showToast({
        title: '请上传医生资格证名！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (photos2 == "") {
      wx.showToast({
        title: '请上传职业证书！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (photos3 == "") {
      wx.showToast({
        title: '请上传身份证！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    } else if (!util.checkIdCard(this.data.idnumber)) { //验证身份证号
      wx.showToast({
        title: '请输入正确的身份证号！',
        icon: 'none',
        duration: 1000,
        mask: true
      })
      return false
    }
     
    //验证手机号码
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号格式有误！',
        icon: 'none',
        duration: 1500
      })
      return false;
    } else {
      wx.showLoading({
        title: '注册中...',
      })
      console.log("提交信息" + photos3[0])//apiutil.json2Form(
      wx.request({
        url: api.Register,
        data: {
          name:name,
          tel:phone,
          idcardno: idnumber,
          sex:that.data.gender,
          certificatetype:that.data.idcard,
          email:email,
          unit:organizename,
          department: department,
          position: position,
          doctorcertificate:photos1[0],
          practicingcertificate: photos2[0],
          idcard: photos3[0],
          synopsis:introduction
        },
        method: 'POST',
        header: {
          'Content-Type': 'application/json'
        },
        success: function (res) {
          wx.hideLoading()
          console.log(res);
          if (res.data.code == 0) {
            wx.showToast({
              title: '注册成功!',
              icon: 'success',
              duration: 3000
            })
            app.globalData.hasLogin = true;
            wx.redirectTo({
              url: '../loginindex/loginindex',
            })
 
          } else {
            wx.showModal({
              title: '注册失败，请检查相应信息！',
              content: res.data.msg,
              showCancel: false
            });
          }
        }
      });
    }
  }

})

